package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.dto.ArticleBehaviorDto;
import com.heima.behavior.dto.BehaviorVo;
import com.heima.behavior.dto.EntryDto;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.entity.ApFollowBehavior;
import com.heima.behavior.entity.ApLikesBehavior;
import com.heima.behavior.entity.ApUnlikesBehavior;
import com.heima.behavior.mapper.ApBehaviorEntryMapper;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.heima.behavior.service.IApFollowBehaviorService;
import com.heima.behavior.service.IApLikesBehaviorService;
import com.heima.behavior.service.IApUnlikesBehaviorService;
import com.heima.common.dto.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP行为实体表,一个行为实体可能是用户或者设备，或者其它 服务实现类
 * </p>
 *
 * @author wsj
 * @since 2022-03-17
 */
@Service
public class ApBehaviorEntryServiceImpl extends ServiceImpl<ApBehaviorEntryMapper, ApBehaviorEntry> implements IApBehaviorEntryService {

    @Override
    public ApBehaviorEntry saveEntry(EntryDto dto) {
        //判断是终端还是用户
        if (dto.getUserId()!=null) {
            //用户登录
            //构建查询条件，看是否存在
            LambdaQueryWrapper<ApBehaviorEntry> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ApBehaviorEntry::getEntryId,dto.getUserId());
            wrapper.eq(ApBehaviorEntry::getType,1);
            ApBehaviorEntry apBehaviorEntry = getOne(wrapper);
            if (apBehaviorEntry!=null) {
                return apBehaviorEntry;
            }else {
                //不存在，则保存
                apBehaviorEntry = new ApBehaviorEntry();
                apBehaviorEntry.setEntryId(dto.getUserId().toString());
                apBehaviorEntry.setType(1);
                apBehaviorEntry.setCreatedTime(new Date());
                save(apBehaviorEntry);
                return apBehaviorEntry;
            }
        }else {
            //终端登录
            //构建查询条件，看是否存在
            LambdaQueryWrapper<ApBehaviorEntry> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ApBehaviorEntry::getEntryId,dto.getEquipmentId());
            wrapper.eq(ApBehaviorEntry::getType,0);
            ApBehaviorEntry apBehaviorEntry = getOne(wrapper);
            if (apBehaviorEntry!=null) {
                return apBehaviorEntry;
            }else {
                //不存在，则保存
                apBehaviorEntry = new ApBehaviorEntry();
                apBehaviorEntry.setEntryId(dto.getEquipmentId().toString());
                apBehaviorEntry.setType(0);
                apBehaviorEntry.setCreatedTime(new Date());
                save(apBehaviorEntry);
                return apBehaviorEntry;
            }
        }
    }

    @Autowired
    private IApFollowBehaviorService followBehaviorService;
    @Autowired
    private IApLikesBehaviorService likesBehaviorService;

    @Autowired
    private IApUnlikesBehaviorService unlikesBehaviorService;
    @Override
    public ResponseResult<BehaviorVo> getBehavior(ArticleBehaviorDto dto) {
        //获取当前用户当前文章行为
        BehaviorVo vo = new BehaviorVo();
        //获取实体
        EntryDto entryDto = new EntryDto();
        entryDto.setUserId(dto.getUserId());
        entryDto.setEquipmentId(dto.getEquipmentId());
        ApBehaviorEntry entry = saveEntry(entryDto);
        //查询行为
        //构建条件,查询是否关注
        LambdaQueryWrapper<ApFollowBehavior> wrapperFollow = new LambdaQueryWrapper<>();
        wrapperFollow.eq(ApFollowBehavior::getEntryId,entry.getId());
        wrapperFollow.eq(ApFollowBehavior::getFollowId,dto.getFollowUserId());
        ApFollowBehavior followBehavior = followBehaviorService.getOne(wrapperFollow);
        if (followBehavior!=null) {
            vo.setIsfollow(followBehavior.getOperation()==0?true:false);
        }
        //构建条件,查询是否点赞
        LambdaQueryWrapper<ApLikesBehavior> wrapperLike = new LambdaQueryWrapper<>();
        wrapperLike.eq(ApLikesBehavior::getEntryId,entry.getId());
        wrapperLike.eq(ApLikesBehavior::getArticleId,dto.getArticleId());
        ApLikesBehavior likesBehavior = likesBehaviorService.getOne(wrapperLike);
        if (likesBehavior!=null) {
            vo.setIslike(likesBehavior.getOperation()==0?true:false);
        }
        //构建条件,查询是否不喜欢
        LambdaQueryWrapper<ApUnlikesBehavior> wrapperUnlike = new LambdaQueryWrapper<>();
        wrapperUnlike.eq(ApUnlikesBehavior::getEntryId,entry.getId());
        wrapperUnlike.eq(ApUnlikesBehavior::getArticleId,dto.getArticleId());
        ApUnlikesBehavior unlikesBehavior = unlikesBehaviorService.getOne(wrapperUnlike);
        if (unlikesBehavior!=null) {
            vo.setIsunlike(unlikesBehavior.getType()==0?true:false);
        }
        vo.setEntryId(entry.getId());
        return ResponseResult.okResult(vo);
    }
}
